package org.devf.fork.imsimulator.view.extend;

import cn.hutool.core.util.StrUtil;
import javafx.scene.Node;
import javafx.scene.control.ListCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import org.devf.fork.imsimulator.model.UserStatus;
import org.devf.fork.imsimulator.service.vo.UserExtendModel;

import java.util.Objects;


/**
 * FriendListCel related
 *
 * @author wanghuafu
 * @since 1.0  2023/9/13 18:30
 */
public class FriendListCel extends ListCell<UserExtendModel> {
    private final HBox content;

    public FriendListCel() {
        content = new HBox();
        content.setSpacing(10);

        //头像
        content.getChildren().add(getAvatarView());

        // 在布局中添加控件
        Text text = new Text();
        text.setId("list-cell-text");
        content.getChildren().add(text);

        // 设置ListCell的图形内容
        setGraphic(content);
    }

    private Node getAvatarView() {
        ImageView imageView = new ImageView();
        imageView.setId("avatar");
        imageView.setFitHeight(30);
        imageView.setFitWidth(30);
        imageView.setPreserveRatio(true);// 保持宽高比
        return imageView;
    }

    @Override
    protected void updateItem(UserExtendModel item, boolean empty) {
        super.updateItem(item, empty);

        if (empty || item == null) {
            // 清除列表项的内容
            setText(null);
            content.setVisible(false);
        } else {
            // 设置列表项的文本和布局
            setText(item.getPhone());
            content.setVisible(true);
            // 在这里可以根据item设置文本或控件的属性
            Text text = (Text) content.lookup("#list-cell-text");
            text.setText(item.getNikname());

            ImageView imageView = (ImageView) content.lookup("#avatar");
            if (StrUtil.isNotBlank(item.getAvatar())){
                imageView.setImage(new Image(item.getAvatar()));
            }

            if (Objects.equals(item.getOnlineStatus(), UserStatus.ONLINE.getCode())){
                text.setFill(Color.BLUE);
            }else{
                text.setFill(Color.BLACK);
            }
        }
    }
}
